home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / NetWarmer / source / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-16  |  6.9 KB  |  366 lines  |  [TEXT/KAHL]

  1. /* © 1988, Bowers Development Corp. */
  2. /* EventLoop.c */
  3.  
  4. #include "Globals.h"
  5. #include "Cursors.h"
  6. #include "DoScrap.h"
  7. #include "Miscellany.h"
  8. #include "WindowAids.h"
  9. #include "Windowing.h"
  10. #include "Dispatcher.h"
  11. #include "MainMenu.h"
  12.  
  13. #include "EventLoop.h"
  14.  
  15. Boolean            hasWNE;
  16.  
  17. /*----------*/
  18. void DoMouseDown    (void);
  19. void DoMouseUp        (void);
  20. void DoKeystroke    (void);
  21. void DoUpdate        (void);
  22. void DoActivate        (void);
  23. void DoDiskEvent    (void);
  24. void DoApp4Event    (void);
  25.  
  26. #define _WaitNextEvent    0xA860
  27.  
  28. /*----------*/
  29. Boolean TrapAvailable  (short        tNumber,
  30.                         TrapType    tType);
  31. Boolean TrapAvailable  (tNumber, tType)
  32. short        tNumber;
  33. TrapType    tType;
  34. {
  35.     return (false);
  36.  
  37. /*----- not yet supported by THINK -----
  38.     return (NGetTrapAddress (tNumber, tType)
  39.             != GetTrapAddress (_Unimplemented));
  40. -----*/
  41. } /*TrapAvailable*/
  42.  
  43. /*----------*/
  44. void SetStackSize (long        stackSize);
  45. void SetStackSize (stackSize)
  46. long            stackSize;
  47. {
  48.     SetApplLimit ((Ptr) ((long) &stackSize - stackSize));
  49. } /*SetStackSize*/
  50.  
  51. /*----------*/
  52. void Initialize        ()
  53. {
  54.     #define sysEnvironsVersion    1
  55.     
  56.     SysEnvRec        environs;
  57.     OSErr            ignoreError;
  58.  
  59.     SetStackSize (15000);
  60.     MaxApplZone ();
  61.     MoreMasters ();
  62.  
  63.     InitGraf (&thePort);
  64.     InitFonts ();
  65.     InitWindows ();
  66.     InitMenus ();
  67.     TEInit ();
  68.     InitDialogs (nil);
  69.     
  70.     FlushEvents (everyEvent, 0);
  71.     
  72.     ignoreError = SysEnvirons (sysEnvironsVersion, &environs);
  73.     if (environs.machineType < 0) {
  74.         hasWNE = false;
  75.     } else {
  76.         hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
  77.     }
  78.     
  79.     inBackground = false;
  80.     
  81.     LoadMenus ();
  82.     LoadCursors ();
  83.     InitScrap (); 
  84.     
  85.     InitGlobals ();
  86.  
  87.     InitModelessDialogs ();    
  88.     InitTitles ();        /*initialize each menu title's module*/
  89. } /*Initialize*/
  90.  
  91. /*----------*/
  92. void DoMouseDown    ()
  93. {
  94.     WindowPtr        whichWindow;
  95.     short            whichPart;
  96.  
  97.     whichPart = FindWindow (curEvent.where, &whichWindow);
  98.     switch (whichPart) {
  99.     case inDesk:
  100.             /*Do nothing*/;
  101.         break;
  102.     case inMenuBar:
  103.             DoMenu (MenuSelect (curEvent.where));
  104.         break;
  105.     case inSysWindow:
  106.             SystemClick (&curEvent, whichWindow);
  107.         break;
  108.     case inContent:
  109.             DoContent (whichWindow);
  110.         break;
  111.     case inDrag:
  112.             DoDrag (whichWindow);
  113.         break;
  114.     case inGrow:
  115.             DoGrow (whichWindow);
  116.         break;
  117.     case inGoAway:
  118.             DoGoAway (whichWindow);
  119.         break;
  120.     case inZoomIn:
  121.             DoZoom (whichWindow, inZoomIn);
  122.         break;
  123.     case inZoomOut:
  124.             DoZoom (whichWindow, inZoomOut);
  125.         break;
  126.     } /*switch*/
  127. } /*DoMouseDown*/
  128.  
  129. /*----------*/
  130. void DoMouseUp        ()
  131. {
  132.     WindowPtr        whichWindow;
  133.     short            whichPart;
  134.  
  135.     whichPart = FindWindow (curEvent.where, &whichWindow);
  136.     /*save event time to check later for double click*/
  137. } /*DoMouseUp*/
  138.  
  139. /*----------*/
  140. void DoKeystroke    ()
  141. {
  142.     short            charCode;
  143.     char            ch;
  144.  
  145.     charCode = curEvent.message & charCodeMask;
  146.     ch = charCode;
  147.     
  148.     if ((curEvent.modifiers & cmdKey) != 0) {
  149.         if (curEvent.what != autoKey) {
  150.             DoMenu (MenuKey (ch));
  151.         }
  152.     } else {
  153.         if (curWindow == nil) {
  154.             SysBeep (1);
  155.         } else {
  156.             TypeInWindow (ch);
  157.         }
  158.     }
  159. } /*DoKeystroke*/
  160.  
  161. /*----------*/
  162. void DoUpdate        ()
  163. {
  164.     GrafPtr            savePort;
  165.     WindowPtr        saveWindow;
  166.     WindowPtr        whichWindow;
  167.  
  168.     GetPort (&savePort);
  169.     whichWindow = (WindowPtr) curEvent.message;
  170.     SetPort (whichWindow);
  171.     saveWindow = curWindow;
  172.     SetInfo (whichWindow);
  173.     BeginUpdate (whichWindow);
  174.         EraseRect (&(whichWindow->portRect));
  175.         DrawControls (whichWindow);
  176.         UpdateContent ();
  177.     EndUpdate (whichWindow);
  178.     SetInfo (saveWindow);    
  179.     SetPort (savePort);
  180. } /*DoUpdate*/
  181.  
  182. /*----------*/
  183. void DoActivate        ()
  184. {
  185.     Boolean            activate;
  186.     WindowPtr        whichWindow;
  187.  
  188.     whichWindow = (WindowPtr) curEvent.message;
  189.     SetPort (whichWindow);
  190.     SetInfo (whichWindow);
  191.     
  192.     activate = ((curEvent.modifiers & activeFlag) != 0);
  193.     if (activate) {
  194.         if ((cur->text) != nil) {
  195.             TEActivate (cur->text);
  196.         }
  197.         ReadDeskScrap ();
  198.     }
  199.     
  200.     HiliteScroll (cur->vScroll, activate);
  201.     HiliteScroll (cur->hScroll, activate);
  202.  
  203.     ActivateContent (activate);
  204.  
  205.     if (!activate) {
  206.         WriteDeskScrap ();
  207.         if (cur->text != nil) {
  208.             TEDeactivate (cur->text);
  209.         }
  210.         SetInfo (nil);
  211.     }
  212. } /*DoActivate*/
  213.  
  214. /*----------*/
  215. void DoDiskEvent ()
  216. {
  217.     #define dlgTop        75                                   
  218.     #define dlgLeft        100
  219.  
  220.     OSErr            ignore;
  221.     Point            where;
  222.  
  223.     if (HiWord (curEvent.message) != noErr) {
  224.         DILoad ();
  225.         SetPt (&where, dlgLeft, dlgTop); 
  226.         ignore = DIBadMount (where, curEvent.message);
  227.         DIUnload ();
  228.     }
  229. } /*DoDiskEvent*/
  230.  
  231. /*----------*/
  232. void DoApp4Event ()
  233. {
  234.     #define suspendResumeMsg    1
  235.     #define resumeMask            1
  236.     
  237.     Boolean            resume;
  238.     WindowPtr        front;
  239.  
  240.     curEvent.what = nullEvent;
  241.     if (BitShift (curEvent.message, -24) == suspendResumeMsg) {
  242.         resume = ((curEvent.message & resumeMask) != 0);
  243.         if (resume) {
  244.             ReadDeskScrap ();
  245.         } else {
  246.             WriteDeskScrap ();
  247.         }
  248.         inBackground = !resume;
  249.         front = FrontWindow ();
  250.         if (front != nil) {
  251.             curEvent.what = activateEvt;
  252.             curEvent.modifiers = LoWord (curEvent.message);
  253.             curEvent.message = (long) front;
  254.         }
  255.     }
  256. } /*DoApp4Event*/
  257.  
  258. /*----------*/
  259. long GetSleep    (void);
  260. long GetSleep    ()
  261. {
  262.     long            sleep;
  263.  
  264.     sleep = 10000;
  265.     if ((!inBackground) && (FrontWindow () != nil)) {
  266.         if (FrontWindow () == curWindow) {
  267.             if (cur->text != nil) {
  268.                 if ((**(cur->text)).selStart == (**(cur->text)).selEnd) {
  269.                     sleep = GetCaretTime ();
  270.                 }
  271.             }
  272.         } else {    /* DA or modeless dialog */
  273.             sleep = GetCaretTime ();
  274.         }
  275.     }
  276.     return (sleep);
  277. } /*GetSleep*/
  278.  
  279. /*----------*/
  280. Boolean GetEvent        (void);
  281. Boolean GetEvent        ()
  282. {
  283.     Boolean            gotEvent;
  284.  
  285.     if (hasWNE) {
  286.         gotEvent = WaitNextEvent (everyEvent, &curEvent,
  287.                                     GetSleep (), cursorRgn);
  288.     } else {
  289.         SystemTask ();
  290.         gotEvent = GetNextEvent (everyEvent, &curEvent);
  291.     }
  292.     return (gotEvent);
  293. } /*GetEvent*/
  294.  
  295. /*----------*/
  296. Boolean DoEvent        (void);
  297. Boolean DoEvent        ()
  298. {
  299.     Boolean            gotEvent;
  300.     DialogPtr        whichDialog;
  301.     short            itemHit;
  302.  
  303.     gotEvent = GetEvent ();
  304.     if (curEvent.what == app4Evt) {
  305.       /* here so event won't get swallowed by IsDialogEvent */
  306.       /* and so suspend/resume can be translated to activate */
  307.         DoApp4Event ();
  308.     }
  309.     if (IsDialogEvent (&curEvent)) {
  310.         if ((curEvent.what == activateEvt)
  311.         ||  (curEvent.what == updateEvt)) {
  312.             whichDialog = (DialogPtr) curEvent.message;
  313.         } else {
  314.             whichDialog = FrontWindow ();
  315.         }
  316.         SetPort (whichDialog);
  317.         if ((Boolean) FilterModeless (whichDialog, &curEvent, &itemHit)) {
  318.             DoModelessItem (whichDialog, itemHit);
  319.         } else if (DialogSelect (&curEvent, &whichDialog, &itemHit)) {
  320.             DoModelessItem (whichDialog, itemHit);
  321.         }
  322.     } else if (gotEvent) {
  323.         switch (curEvent.what) {
  324.         case mouseDown:
  325.                 DoMouseDown ();
  326.             break;
  327.         case mouseUp:
  328.                 DoMouseUp ();
  329.             break;
  330.         case keyDown:
  331.         case autoKey:
  332.                 DoKeystroke ();
  333.             break;
  334.         case updateEvt:
  335.                 DoUpdate ();
  336.             break;
  337.         case activateEvt:
  338.                 DoActivate ();
  339.             break;
  340.         case diskEvt:
  341.                 DoDiskEvent ();
  342.             break;
  343.         } /*switch*/
  344.     }
  345.     return (gotEvent);
  346. } /*DoEvent*/
  347.  
  348. /*----------*/
  349. void MainLoop    ()
  350. {
  351.     UpdateMenus ();
  352.  
  353.     quittingTime = false;    
  354.     while (!quittingTime) {
  355.         ShapeCursor ();
  356.         if (cur->text != nil) {
  357.             TEIdle (cur->text);
  358.         }
  359.         if (DoEvent ()) {
  360.             UpdateMenus ();
  361.         }
  362.     } /*while*/
  363.     
  364.     WriteDeskScrap ();
  365. } /*MainLoop*/
  366.